package great.easychat.help.dialog;

import android.app.Activity;
import android.app.AlertDialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;

import great.easychat.help.R;

import static android.content.Context.INPUT_METHOD_SERVICE;


/**
 * 邀请码填写弹窗
 */
public class InviteCodeDialog {
    private AlertDialog dialog = null;

    public interface Callback {
        void onOK(String code);
    }

    private Callback mcallback;


    /**
     * @param context  上下文
     * @param callback 回调
     */
    Activity activity;
    public void show(Activity context,final Callback callback) {
        if (dialog == null) {
            activity = context;
            dialog = new AlertDialog.Builder(context).create();
            mcallback = callback;
            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            dialog.setCanceledOnTouchOutside(true);// 设置点击屏幕Dialog是否消失
            dialog.setCancelable(true);// 返回按钮
            if (!context.isFinishing()) {
                dialog.show();
            }
            Window window = dialog.getWindow();
            WindowManager.LayoutParams lp = window.getAttributes();
            window.setAttributes(lp);
            window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            window.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
            window.setContentView(R.layout.dialog_invite);
            window.clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
            Button btnSure = (Button) window.findViewById(R.id.btnSure);
            final EditText et_code = (EditText) window.findViewById(R.id.et_code);
            btnSure.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (!et_code.getText().toString().isEmpty()) {
                        callback.onOK(et_code.getText().toString());
//                        cancelDialog();
                        hideSoftInput(et_code);
                    }
                }
            });
        }
    }

    private void hideSoftInput(EditText editText) {
        InputMethodManager manager = (InputMethodManager) activity.getSystemService(INPUT_METHOD_SERVICE);
        if (manager != null && manager.isActive()) {
            manager.hideSoftInputFromWindow(editText.getApplicationWindowToken(), 0);
        }
    }

    /**
     * 取消窗口
     */
    public void cancelDialog() {
        try {
            if (dialog != null) {
                dialog.dismiss();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void showDialog() {
        if (dialog != null && !dialog.isShowing()) {
            dialog.show();
        }
    }

}
